/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import blueprint4j.Constants;
import blueprint4j.utils.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.swing.*;

public class DataPairFieldDate extends DataPairDefault implements FieldBinding, ActionListener {
    private BindDateInterface field;
    private JFormattedTextField text_field = null;
    private SimpleDateFormat date_format = new SimpleDateFormat(Settings.getString("gui.gui.db_tools.date_format", "dd/MM/yyyy"));
    private JButton button=null;
 //   private boolean editable = false;
    private VectorDataPairSetListener listeners = new VectorDataPairSetListener();
    
    public DataPairFieldDate(Binder binder,BindDateInterface p_field) {
        this(binder,p_field, true);
    }
    
    public DataPairFieldDate(Binder binder,BindDateInterface p_field, boolean editable) {
        super(binder,p_field);
 //       this.editable = editable;
        text_field = new JFormattedTextField(date_format);
        text_field.setColumns(7);
        
        field = p_field;
        setLabel(new JLabel(field.getDescription()));
        Box box = Box.createHorizontalBox();
        box.add(text_field);
        try {
            button = new AnimatingButton(null, new String[]{Constants.ICON_BASE+"actions/1day.png"}, 1);
        } catch (IOException ioe) {
            ioe.printStackTrace();
            Log.critical.out( ioe);
            System.exit( -1);
        }
        button.setEnabled(editable);
        box.add(button);
        box.add(Box.createHorizontalStrut(10));
        box.add(new JLabel(Settings.getString("gui.gui.db_tools.date_format", "dd/MM/yyyy")));
        button.setHorizontalAlignment(AbstractButton.CENTER);
        button.setVerticalAlignment(AbstractButton.CENTER);
        button.setHorizontalTextPosition(AbstractButton.CENTER);
        button.setVerticalTextPosition(AbstractButton.CENTER);
        button.setIconTextGap(0);
        button.setMargin(new Insets(0, 0, 0, 0));
        button.setFocusable(false);
        button.addActionListener(this);
        button.setBorder(null);
        setComponent(box);
        text_field.setEditable(editable);
    }
    
    public void addDataPairSetListener(DataPairSetListener listener) {
        listeners.add(listener);
    }
    
    public boolean isChanged() {
        Date value = field.get();
        if (value == null) {
            return text_field.getText() != null;
        } else {
            try {
                return !value.equals(date_format.parse(text_field.getText()));
            } catch (ParseException pe) {
                return false;
            }
        }
    }
    
    public void intoControl() {
        Date date = field.get();
        if (date != null) {
            text_field.setValue(date);
        } else {
            text_field.setValue(null);
        }
    }
    
    public void intoBindable() {
        try {
            String date = text_field.getText();
            if (date != null) {
                field.set(date_format.parse(date));
            } else {
                field.set(null);
            }
        } catch (ParseException pe) {}
    }
    
    public Bindable getBindable() {
        return field.getBindable();
    }
    
    public void actionPerformed(ActionEvent e) {
        String sdate = text_field.getText();
        Date date = null;
        if (sdate != null && !sdate.equals("")) {
            try {
                date = date_format.parse(sdate);
            } catch (ParseException pe) {
                Log.debug.out( pe);
            }
        }
        date = JCalendar.showDateDialogue(button, field.getDescription(), date);
        if (date != null) {
            text_field.setValue(date);
            listeners.dateSet();
        }
    }
    
    public BindFieldInterface getBindField() {
        return (BindFieldInterface) field;
    }
    
    public void setBindField(BindFieldInterface p_field) {
        field = (BindDateInterface) p_field;
    }
    
    public void resetControl() {
        text_field.setText("");
    }
    
    public static interface DataPairSetListener {
        void dateSet();
    }
    
    public class VectorDataPairSetListener {
        private Vector<DataPairSetListener> store=new Vector<DataPairSetListener>();
        
        public DataPairSetListener get(int pos){
            return (DataPairSetListener)store.get(pos);
        }
        
        public void add(DataPairSetListener item){
            store.add(item);
        }
        
        public boolean remove(DataPairSetListener item){
            return store.remove(item);
        }
        
        public DataPairSetListener remove(int pos){
            return (DataPairSetListener) store.remove(pos);
        }
        
        public int size(){
            return store.size();
        }
        
        public void dateSet() {
            for (int i =0;i < size();i++) {
                get(i).dateSet();
            }
        }
    }
}

